package com.maxh.interceptor;

import com.maxh.util.ReturnData;
import com.maxh.util.ReturnError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@ResponseBody
@ControllerAdvice(basePackages = {"com.maxh.controller","com.maxh.service"})
public class CustomExceptionAdvice {
    /*一旦捕获自定义异常  记得slf4j 打印一下日志记录*/
    private  static  final Logger logger=LoggerFactory.getLogger(CustomExceptionAdvice.class);
    @ExceptionHandler(value = MyException.class)
    public ReturnError userExceptionHandle(HttpServletRequest request, MyException e){
        logger.error("错误码:{},错误信息:{},用户信息异常：Host:{},invoke URL:{}",
                e.getCode(),e.getMessage(),request.getRemoteHost(),request.getRequestURL());
        return new ReturnError(e.getCode(),e.getMessage());
    }


}
